package com.realdolmen.whiteyellowcross.service;

import java.util.*;

import com.realdolmen.whiteyellowcross.datastore.WaitWorkShiftDayPlanDataStore;
import com.realdolmen.whiteyellowcross.memory.datastore.WaitMonthPlanningMemory;
import com.realdolmen.whiteyellowcross.memory.datastore.WaitWorkShiftPlanMemory;
import com.realdolmen.whiteyellowcross.model.Personel;
import com.realdolmen.whiteyellowcross.model.WaitMonthPlanning;
import com.realdolmen.whiteyellowcross.model.WaitWorkShift;
import com.realdolmen.whiteyellowcross.model.WaitWorkShiftDayPlan;
import com.realdolmen.whiteyellowcross.util.DateTimeHelper;

public class PlanningPersistenceBasedService implements PlanningService {

    private WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore = null;

    private static PlanningService planningService;

    private PlanningPersistenceBasedService(
            WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore) {
        this.waitWorkShiftDayPlanDataStore = waitWorkShiftDayPlanDataStore;
    }

    public static PlanningService getInstance(
            WaitWorkShiftDayPlanDataStore waitWorkShiftDayPlanDataStore) {
        if (planningService == null) {
            synchronized (PersonelPersistenceBasedService.class) {

                if (planningService == null) {
                    planningService = new PlanningPersistenceBasedService(
                            waitWorkShiftDayPlanDataStore);

                }
            }
        }
        return planningService;
    }

    @Override
    public WaitMonthPlanning getWaitMonthPlanning(int year, int month, List<Personel> personelList, List<WaitWorkShift> workShiftList, boolean createDefault) {

        WaitMonthPlanning waitMonthPlanning = new WaitMonthPlanning(new Long(year),
                new Long(month));

        int daysInMonth = DateTimeHelper.getDaysInMonth(month - 1, year);


        for (int i = 0; i < daysInMonth; i++) {

            Calendar waitWorkShiftDayCalendar = Calendar.getInstance();
            waitWorkShiftDayCalendar.set(Calendar.YEAR, year);
            waitWorkShiftDayCalendar.set(Calendar.MONTH, month - 1);
            waitWorkShiftDayCalendar.set(Calendar.DATE, i + 1);
            Date waitWorkShiftDayDate = waitWorkShiftDayCalendar.getTime();

            List<WaitWorkShiftDayPlan> waitWorkShiftDayPlanningList = waitWorkShiftDayPlanDataStore.getWaitWorkShiftDayPlan(waitWorkShiftDayDate);

            if (waitWorkShiftDayPlanningList == null || waitWorkShiftDayPlanningList.size() == 0) {
                createRandomWaitWorkShiftDayPlan(waitWorkShiftDayDate, personelList, workShiftList);
                waitWorkShiftDayPlanningList = waitWorkShiftDayPlanDataStore.getWaitWorkShiftDayPlan(waitWorkShiftDayDate);
            }

            waitMonthPlanning.addWaitDayPlanning(i + 1, waitWorkShiftDayPlanningList);

        }


        WaitMonthPlanning waitPlanning = WaitMonthPlanningMemory
                .getWaitPlanning(year, month);
        return waitPlanning;
    }

    private void createRandomWaitWorkShiftDayPlan(Date waitWorkShiftDayDate, List<Personel> personelList, List<WaitWorkShift> workShiftList) {
        for (WaitWorkShift waitWorkShift : workShiftList) {
            Random random = new Random();
            Personel randomPersonel = personelList.get(random.nextInt(personelList.size()));
            waitWorkShiftDayPlanDataStore.insertWaitWorkShiftDayPlan(waitWorkShiftDayDate, randomPersonel.getId(), waitWorkShift.getId());
        }

    }

    public void updateWaitMonthPlanning(String waitWorkShiftDayPlanId,
                                        String personelId) {

        waitWorkShiftDayPlanDataStore.updateWaitWorkShiftDayPlan(waitWorkShiftDayPlanId, personelId);


    }


}
